﻿using MediatR;
using MeetingRoomBooking.Application.Common.Models;
using MeetingRoomBooking.Application.Rooms.Commands;
using MeetingRoomBooking.Application.Rooms.Queries;
using MeetingRoomBooking.Application.Users.Commands;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WebAPI.Controllers;
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly IMediator _mediator;

    public UserController(IMediator mediator)
    {
        _mediator = mediator;
    }

    [HttpPost]
    [Route("login")]
    public async Task<LoginViewModel> Login([FromBody] LoginCommand command)
    {
        return await _mediator.Send(command);
    }

    [HttpPost]
    [Route("create")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public async Task<int> CreateUser([FromBody] CreateUserCommand command)
    {
        return await _mediator.Send(command);
    }

    [HttpPut]
    [Route("update")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public async Task UpdateUser([FromBody] UpdateUserCommand command)
    {
        await _mediator.Send(command);
    }

    [HttpDelete]
    [Route("users/{id}")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public async Task DeleteUser(int id)
    {
        var command = new DeleteUserCommand { Id = id };
        await _mediator.Send(command);
    }

    [HttpGet]
    [Route("users/{pageNumber}/{pageSize}")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public async Task<PaginatedList<UserViewModal>> GetRoomsWithPaginated(int pageNumber, int pageSize)
    {
        var queryParam = new GetUsersWithPaginationQuery() { PageNumber = pageNumber, PageSize = pageSize };
        var users = await _mediator.Send(queryParam);

        return users;
    }
}
